package com.yanqu.road.dao.impl.activity.shuihucard;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.shuihu.ShuiHuNotice;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShuiHuNoticeDaoImpl extends TempDataObjectDao<ShuiHuNotice> {

    public static String getShuiHuNoticeSql = "select * from t_c_activity_shuihu_notice where activityId=? order by roundNo asc";

    public List<ShuiHuNotice> getShuiHuNoticeList(int activityId) {
        List<ShuiHuNotice> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getShuiHuNoticeSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ShuiHuNotice shuiHuNotice = parse(rs);
                list.add(shuiHuNotice);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateShuiHuNoticeSql = "update t_c_activity_shuihu_notice set roundNo=?,userId=?,userBaseInfo=?,cardId=?,createTime=?";

    @Override
    protected boolean updateData(ShuiHuNotice shuiHuNotice) {
        shuiHuNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getRoundNo()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuNotice.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuNotice.getCreateTime()));
        if (getDb().executeNoneQuery(updateShuiHuNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertShuiHuNoticeSql = "insert into t_c_activity_shuihu_notice(activityId,roundNo,userId,userBaseInfo,cardId,createTime) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(ShuiHuNotice shuiHuNotice) {
        shuiHuNotice.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getRoundNo()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuNotice.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(shuiHuNotice.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, shuiHuNotice.getCardId()));
        para.put(i++, new DbParameter(Types.BIGINT, shuiHuNotice.getCreateTime()));
        if (getDb().executeNoneQuery(insertShuiHuNoticeSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected ShuiHuNotice parseData(ResultSet rs) throws SQLException {
        ShuiHuNotice shuiHuNotice = new ShuiHuNotice();
        shuiHuNotice.setActivityId(rs.getInt("activityId"));
        shuiHuNotice.setRoundNo(rs.getInt("roundNo"));
        shuiHuNotice.setUserId(rs.getLong("userId"));
        String userBaseInfo = rs.getString("userBaseInfo");
        shuiHuNotice.setUserBaseInfo(StringUtils.isNullOrEmpty(userBaseInfo) ? new UserBaseInfo() : JSONObject.parseObject(userBaseInfo, new TypeReference<UserBaseInfo>() {
        }));
        shuiHuNotice.setCardId(rs.getInt("cardId"));
        shuiHuNotice.setCreateTime(rs.getLong("createTime"));
        return shuiHuNotice;
    }

}
